/*
 * Copyright (C) 2009 Chang Min Lee <ch5ngmn@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. (/COPYING)
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "IoSessionServerCreator.h"
#include "IoSessionLocator.h"
#include "IoSessionManager.h"

int 
IoSessionServerCreator::open (int argc, ACE_TCHAR *argv[])
{
	if (0 == IoServerCreator::open (argc, argv)) 
  {
		this->server_name_ = SESSION_SERVER_NAME;
    this->maxSession_ = 10;
    return 0;
  }
  return -1;
}

void IoSessionServerCreator::close()
{
}

void 
IoSessionServerCreator::activate_poa (PortableServer::POA_ptr parent_poa)
{
	// Create the policies and assign them for the sessionPOA
  CORBA::PolicyList policies(3);
  policies.length(3);

  policies[0] = parent_poa->create_id_assignment_policy(PortableServer::USER_ID);
  policies[1] = parent_poa->create_request_processing_policy(PortableServer::USE_SERVANT_MANAGER);
  policies[2] = parent_poa->create_servant_retention_policy(PortableServer::NON_RETAIN);

  // Create the POA with these policies
  poa_ = parent_poa->create_POA(SESSION_POA_NAME, 
																								parent_poa->the_POAManager(), 
																								policies);
	ACE_ASSERT (false == CORBA::is_nil(poa_.in()));

  // Destroy the policy objects
  for (CORBA::ULong i = 0; i != policies.length(); ++i) {
    policies[i]->destroy();
  }

	ACE_DEBUG((LM_DEBUG, ACE_TEXT("POA (%s) is activated\n"), poa_->the_name()));
}

void 
IoSessionServerCreator::activate_server (CosNaming::NamingContextExt_ptr nctx)
{
	ACE_ASSERT (false == CORBA::is_nil(nctx));

  // Create our Session's ServantLocator.
  PortableServer::ServantLocator_var locator;
	ACE_NEW_NORETURN (locator, IoSessionLocator(session_manager_));

  // Set the Servant Manager with the sessionPOA.
  poa_->set_servant_manager(locator.in());

	ACE_DEBUG((LM_DEBUG, "Server (%s) is activated\n", server_name_.c_str()));
}

//ACE_FACTORY_DEFINE (TAONET, IoSessionServerCreator)